/**
 *
 * 画单图程序，功能待添加
 *
 */
package org.sysmo.server.graph;

import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import java.awt.Graphics;

/**
 *
 */
public class jeruGraphics {
    BufferedImage image;

// 创建 jpg 文件到指定路径下
    public void createJpg(OutputStream path) {
        try
        {
            BufferedOutputStream bos = new BufferedOutputStream(path);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
            encoder.encode(image);
            bos.close();
        }
        catch(FileNotFoundException fnfe)
        {
            System.out.println(fnfe);
        }
        catch(IOException ioe)
        {
            System.out.println(ioe);
        }
    }

    public void LineGraph(Vector<Point> GraphPoint, jeruGraphics jg, int max , Color color)
    {
        int height = 200;
        int xLength = 550, yLength = 150;
        int count = GraphPoint.size();
        double param1 = (double)yLength/ (double)max;
        int i;
        double temp = 0;
        long time,Time;

        
        Graphics g = jg.image.getGraphics();
        Time = GraphPoint.elementAt(count-1).getTime().getTime()-GraphPoint.elementAt(0).getTime().getTime();

        g.setColor(color);
        // 折线图表示方法
            int yTo; int yFrom = GraphPoint.elementAt(0).getValue();
            for (i = 1; i < count; i++){
                yTo = GraphPoint.elementAt(i).getValue();
                time = GraphPoint.elementAt(i).getTime().getTime()-GraphPoint.elementAt(i-1).getTime().getTime();
                g.drawLine((int)(11+temp),(int)(height-40-yFrom*param1),(int)(11+temp+(double)time*(double)xLength/(double)Time),(int)(height-40-yTo*param1));
                yFrom = yTo;
                temp+=(double)time*(double)xLength/(double)Time;
            }
        }

    public void SqureGraph(Vector<Point> GraphPoint, Vector<Point> LastPoint, jeruGraphics jg, int max, Color color)
    {
        int height = 200;
        int xLength = 550, yLength = 150;
        int count = GraphPoint.size();
        double param = (double)yLength/(double)max;
        int i;
        double temp=0;
        long time,Time;

        Graphics g = jg.image.getGraphics();
        Time = GraphPoint.elementAt(count-1).getTime().getTime()-GraphPoint.elementAt(0).getTime().getTime();
        g.setColor(color);


        //面积图表示方法
        int ynTo; int ynFrom = GraphPoint.elementAt(0).getValue();
        int yoTo; int yoFrom = LastPoint.elementAt(0).getValue();

        for(i = 1; i< count;i++){
            ynTo = GraphPoint.elementAt(i).getValue();
            yoTo = LastPoint.elementAt(i).getValue();
            time = GraphPoint.elementAt(i).getTime().getTime()-GraphPoint.elementAt(i-1).getTime().getTime();

            int[] xPoints = {(int)(11+temp),(int)(11+temp),(int)(11+temp+(double)time*(double)xLength/(double)Time),(int)(11+temp+(double)time*(double)xLength/(double)Time)};
            int[] yPoints = {(int)(height-40-(yoFrom+ynFrom)*param),(int)(height-40-yoFrom*param),(int)(height-40-yoTo*param),(int)(height-40-(yoTo+ynTo)*param)};

            g.fillPolygon(xPoints, yPoints, 4);

            yoFrom = yoTo;
            ynFrom = ynTo;
            temp+=(double)time*(double)xLength/(double)Time;

        }
    }
    
    public BufferedImage getImage()
    {
        return image;
    }

}